package arithmetic2;

import utils.PrintUtil;

import java.util.List;

/**
 * @author macpro
 * @date 2019-08-08
 * @function
 */
public class T_021_MergeTwoSortedLists {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node4 = new ListNode(4);

        node1.next=node2;
        node2.next=node4;

        ListNode node11 = new ListNode(1);
        ListNode node22 = new ListNode(2);
        ListNode node44 = new ListNode(4);
        node11.next=node22;
        node22.next=node44;

        PrintUtil.print(mergeTwoLists(node1,node11));
    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head =new ListNode(0);

        ListNode temp = head;
        while (l1!=null && l2!=null){
//            System.out.println(temp.val);
//            System.out.println(l1.val+"  "+l2.val);
            if (l1.val>l2.val){
                temp.next = l2;
                l2 = l2.next;
//                System.out.println("1111  l2 " +l2.val);
            }else {
//                System.out.println("2222  l1 " +l1.val);
                temp.next=l1;
                l1 = l1.next;
            }
            temp = temp.next;
        }

        if (l1 !=null ){
            temp.next=l1;
        }
        if (l2!=null){
            temp.next=l2;
        }

        return head.next;

    }
}
